package com.ljx.seckill.util;

import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;

/**
 * @Author: LiJunXi
 * @Description: 单机版Redis分布式锁
 * 存在问题：
 * 1、误删情况
 * 2、过期时间如何保证大于执行时间
 * @Date: 2025-01-10  22:26
 */
public class RedisUtilV1 implements RedisLock{

    StringRedisTemplate stringRedisTemplate;

    public RedisUtilV1(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public Boolean lock(String key, long time, TimeUnit timeUnit) {
        return stringRedisTemplate.opsForValue().setIfAbsent(key, "1", time, timeUnit);
    }

    public Boolean unlock(String key) {
        return stringRedisTemplate.delete(key);
    }

}
